(function () {
    var $tbody = $("#list-body"),
    	$tbody1=$("#list1-body"),
    	$searchForm = $("#search-form"),
    	$feedbackDate = $("#feedbackdate"),
        trHtml = undefined,
        stations = [],
        curPage = 1;
        curPage1 = 1;
    var _this = {
    	// 表格初始化 
        list : function(params){
        	params = params || {};
//          params = $searchForm.serializeObject();
            params.page = curPage;
            params.size=10;
            App.ajax({
                data: params,
                resUrl: "ygxStudent/list",		// 分页查询接口
                callback: function (rb) {
                    if(!rb.status) return;
                    var rbData = rb.data;
                    $tbody.empty();
                    $.each(rbData.list||[], function(i, d){
                        var $tr = $(trHtml);
                        if( rbData.pageNumber == 1 ){
                        	d.num = i + 1;
                        }else{
                        	d.num = (rbData.pageNumber - 1) * 10 + i + 1;
                        }
                        $tr.setHtml(d).data(d);
                        $tr.find("button").data("id", d.id);
                        $tbody.append($tr);
                    });
                    require(['laypage'], function (laypage) {
                        laypage({
                            cont: 'page-body', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                            pages:rbData.totalPage, //通过后台拿到的总页数
                            curr: rbData.pageNumber || 1, //当前页
                            jump: function (obj, first) { //触发分页后的回调
                                curPage = obj.curr;
                                if (!first) {
                                    _this.list();
                                }
                            }
                        });
                    });
                }
            });
        },
         orderdetail : function(){
        	var params = {};
//			params.lineId = lineId;
			params.page = curPage1;
			params.size = 10;
			params = $.extend({}, params);
        	App.ajax({
				data: params,
				resUrl: "/ygxStudent/list",
				callback: function(rb) {
					if(!rb.status) return;
                    var rbData = rb.data;
                    $("#list1-body").empty().show();
                    $.each(rbData.list||[], function(i, d){
                        var $tr = $(trHtml1);
                        if( rbData.pageNumber == 1 ){
                        	d.num = i + 1;
                        }else{
                        	d.num = (rbData.pageNumber - 1) * 10 + i + 1;
                        }
                        $tr.setHtml(d).data(d);
                        $tr.find("button").data("id", d.id);
                        $("#list1-body").append($tr);
                    });
                    require(['laypage'], function (laypage) {
                        laypage({
                            cont: 'page1-body', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                            pages:rbData.totalPage, //通过后台拿到的总页数
                            curr: rbData.pageNumber || 1, //当前页
                            jump: function (obj, first) { //触发分页后的回调
                                curPage1 = obj.curr;
                                if (!first) {
                                	_this.orderdetail();
                                }
                            }
                        });
                    });
				}
			});
        },
        
        // 事件初始化
        initEvent : function(){
			$("#startDate").dateRangePicker({
				autoClose: false,
				singleDate: true,
				timePicker: false,
				startOfWeek: 'Wednesday',
				format: 'YYYY-MM-DD',
				time: {
					enabled: false 
				}, 
				callback: function(){
					console.log(111)
               		if( _this.dayDiff( $("#startDate").val().split(' ')[0] , $("#endDate").val().split(' ')[0] ) <= 0 ){
               			$("#endDate").val('');
               		}
               		if( $("#startDate").val().split(' ')[0] != '' ){
	               		var startTime = $("#startDate").val().split(' ')[0];
	               		$("#endDate").dateRangePicker(
							{
								startDate: startTime,
			    				endDate: '2020-12-31',
								autoClose: false,
								singleDate: true,
								timePicker : false,
								startOfWeek: 'Wednesday',
								format:'YYYY-MM-DD',
								time: {
				                    enabled: false
				               },
				               callback: function(){
				               }
							}
						);
               		}
               	}
			});
			$("#endDate").dateRangePicker(
				{
					autoClose: false,
					singleDate: true,
					timePicker : false,
					startOfWeek: 'Wednesday',
					format:'YYYY-MM-DD',
					time: {
	                    enabled: false
	               	},
	               	callback: function(){
	                }
				}
			);
			App.delegate({
				btntype: "[btn-type='details']",
				callback: function(data) {
					var lineId = data.id;
					var opt = {
						content: $("#orderdetail").html(),
						submit: function(from) {
							App.closeEntitySlider();
						},
						callback: function(e) {
							$("#entitySlider .detail-brand").find("vl").html("订单详情");
							sttrHtml = $("#stlist-body").html();
							var $form = $(e);
							trHtml1 = $("#list1-body").html(),
							_this.orderdetail();
						}
					};
					App.drawDetailPanel(opt);
				}
			});
			// 导出订单
			$("#exportAll").click(function(){
                var _d = $.extend({}, postData);
                _d.export = $(this).data("export");
                _d.statType = "export";
                var param = "?";
                $.each(_d, function(k,v){
                    param += k+"="+v+"&";
                });
                window.open("/stat/planStatExport"+param);
            });
        	// 查询初始化
            $searchForm.submit(function(){
            	var params = $searchForm.serializeObject();
            	var feedbackDate = $feedbackDate.val()||"";
				if(feedbackDate.indexOf("to")!=-1){
					params.beginDt = feedbackDate.split(" to ")[0];
					params.endDt = feedbackDate.split(" to ")[1];
				}
				if(!params.beginDt || !params.endDt){
					layer.msg("请正确选择反馈日期区间", { icon: 2 });
					return;
				}
				curPage = 1;
				_this.list(params);
				return false;
			});
        },
        /**
		 * 两个日期的天数差，参数格式：'YYYY-MM-DD HH:mm:ss'
		 * @param {Object} startDate 开始日期	
		 * @param {Object} endDate 结束日期
		 */
		dayDiff: function( startDate, endDate ){
			var beginTime = startDate;
			var endTime = endDate;
			var beginDate = new Date(beginTime);
			var endDate = new Date(endTime);
			var day = (endDate.getTime() - beginDate.getTime() ) / ( 24*60*60*1000 ) + 1;
			return day;
		},
        init : function () {
            trHtml = $tbody.html();
            $tbody.empty().show();
            _this.initEvent();
            _this.list();
        }
    }
    _this.init(); //页面初始化入口设置
})();
